using BaWei.Rabbit.Domain;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;

namespace BaWei.Rabbit.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly RabbitDbContext _dbContext;

        public ValuesController(RabbitDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        [HttpGet]
        public async Task<IActionResult> StudentAll()
        {
            string cc= "aaaa";
            string aa = "asdfsdfsd";
            string bb = "agbabab";

            //先判断Redis 当中是否有数据库
            var list = RedisHelper.Get<List<Student>>("key:students");
            if (list!= null && list.Count > 0)
            {
                return Ok(list);
            }
            //读取数据库

           var result =await  _dbContext.Students.ToListAsync();
            //数据同步到Redis当中
            //到时间后自动删除
           await RedisHelper.SetAsync("key:students", result, TimeSpan.FromMinutes(10));

            string aabbvvvva = "asdfsadfs";


            string ccacaca= "asdfsadfs";

            return Ok(result);
        }

        [HttpPost]
        public async Task<IActionResult> AddStudent(Student student)
        {
            await RedisHelper.DelAsync("key:students");
            await _dbContext.Students.AddAsync(student);
            await _dbContext.SaveChangesAsync();
            await Task.Delay(50);  //等待
            await RedisHelper.DelAsync("key:students");
            return Ok();
        }
    }
}
